package com.assignwork.service.impl;

import com.assignwork.dto.WorkArrangeFixedMachineSubmitDTO;
import com.assignwork.dto.WorkArrangeShipCabinInfoSubmitDTO;
import com.assignwork.dto.WorkArrangeShipUnderMachineSubmitDTO;
import com.assignwork.entity.WorkArrangeFixedMachine;
import com.assignwork.entity.WorkArrangeMachines;
import com.assignwork.entity.WorkArrangeShipCabinInfo;
import com.assignwork.mapper.WorkArrangeShipCabinInfoMapper;
import com.assignwork.service.IWorkArrangeShipCabinInfoService;
import com.assignwork.service.IWorkArrangeShipUnderMachineService;
import com.assignwork.vo.StartedCabinVO;
import com.assignwork.vo.WorkArrangeShipCabinInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName WorkArrangeShipCabinInfoServiceImpl
 * @Description 航运配工作业舱接口实现
 * @Author dujianyu
 * @Date 2023/12/22 7:55
 */
@Service
@AllArgsConstructor
public class WorkArrangeShipCabinInfoServiceImpl extends BaseServiceImpl<WorkArrangeShipCabinInfoMapper, WorkArrangeShipCabinInfo> implements IWorkArrangeShipCabinInfoService {

    private IWorkArrangeShipUnderMachineService underMachineService;

    @Override
    public boolean submitWaShipCabinInfo(Long arrangeId, List<WorkArrangeShipCabinInfoSubmitDTO> data) {
        Set<Long> exclude = new HashSet<>();
        if (Func.isNotEmpty(data)) {
            exclude = data.stream().map(WorkArrangeShipCabinInfoSubmitDTO::getId).filter(Objects::nonNull).collect(Collectors.toSet());
        }
        boolean removeOldSuccess = removeByArrangeId(arrangeId, exclude);
        boolean submitUnderMachines = true;
        if (!Func.isNull(data) && !Func.isEmpty(data)) {
            Set<Long> exclude2 = data.stream().filter(i -> i.getUnderMachines() != null && !i.getUnderMachines().isEmpty()).flatMap(i -> i.getUnderMachines().stream().map(WorkArrangeShipUnderMachineSubmitDTO::getId).filter(Objects::nonNull)).collect(Collectors.toSet());
            underMachineService.removeByArrangeId(arrangeId, exclude2);
            for (WorkArrangeShipCabinInfoSubmitDTO item : data) {
                WorkArrangeShipCabinInfo newCabinInfo = new WorkArrangeShipCabinInfo();
                newCabinInfo.setArrangeId(arrangeId);
                newCabinInfo.setPlanId(item.getPlanId());
                newCabinInfo.setAssemblyLineId(item.getAssemblyLineId());
                newCabinInfo.setSupportMachineKind(item.getSupportMachineKind());
                newCabinInfo.setSupportMachineCorp(item.getSupportMachineCorp());
                newCabinInfo.setFixedMachineId(item.getFixedMachineId());
                newCabinInfo.setGroupFg(item.getGroupFg());
                newCabinInfo.setRealCabin1(item.getRealCabin1());
                newCabinInfo.setRealHatch1(item.getRealHatch1());
                newCabinInfo.setRealCabinAndHatch1(item.getRealCabinAndHatch1());
                newCabinInfo.setRealCabin2(item.getRealCabin2());
                newCabinInfo.setRealHatch2(item.getRealHatch2());
                newCabinInfo.setRealCabinAndHatch2(item.getRealCabinAndHatch2());
                newCabinInfo.setPlanTon(item.getPlanTon());
                newCabinInfo.setCabinLeftTon(item.getCabinLeftTon());
                newCabinInfo.setShipMachineryCount(item.getShipMachineryCount());
                if (item.getId() != null) {
                    newCabinInfo.setId(item.getId());
                }

                super.saveOrUpdate(newCabinInfo);
                submitUnderMachines = underMachineService.submitWaShipUnderMachine(arrangeId, newCabinInfo.getId(), item.getUnderMachines());
            }
            return removeOldSuccess && submitUnderMachines;
        } else {
            return removeOldSuccess;
        }
    }

    private boolean removeByArrangeId(Long arrangeId, Set<Long> exclude) {
        QueryWrapper<WorkArrangeShipCabinInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(WorkArrangeShipCabinInfo::getId)
                .eq(WorkArrangeShipCabinInfo::getArrangeId,arrangeId)
                .notIn(Func.isNotEmpty(exclude), WorkArrangeShipCabinInfo::getId, exclude)
                .eq(WorkArrangeShipCabinInfo::getIsDeleted,0);
        List<WorkArrangeShipCabinInfo> entities = baseMapper.selectList(queryWrapper);
        if (!Func.isEmpty(entities)) {
            List<Long> ids = entities.stream().map(WorkArrangeShipCabinInfo::getId).collect(Collectors.toList());
            return super.deleteLogic(ids);
        }
        return true;
    }

    @Override
    public boolean removeByArrangeId(Long arrangeId) {
        QueryWrapper<WorkArrangeShipCabinInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(WorkArrangeShipCabinInfo::getId)
                .eq(WorkArrangeShipCabinInfo::getArrangeId,arrangeId)
                .eq(WorkArrangeShipCabinInfo::getIsDeleted,0);
        List<WorkArrangeShipCabinInfo> entities = baseMapper.selectList(queryWrapper);
        if (!Func.isEmpty(entities)) {
            List<Long> ids = entities.stream().map(WorkArrangeShipCabinInfo::getId).collect(Collectors.toList());
            return super.deleteLogic(ids);
        }
        return true;
    }

    @Override
    public List<WorkArrangeShipCabinInfoVO> infoByArrangeId(Long arrangeId) {
        List<WorkArrangeShipCabinInfoVO> data = baseMapper.selectInfoByArrangeId(arrangeId);
        return dataProcess(data);
    }

    @Override
    public BigDecimal allCabinLeftTon(long planId, long assemblyLineId) {
        return baseMapper.selectAllCabinLeftTon(planId, assemblyLineId);
    }

    private List<WorkArrangeShipCabinInfoVO> dataProcess(List<WorkArrangeShipCabinInfoVO> data) {
        for (WorkArrangeShipCabinInfoVO vo : data) {
            vo.setSupportMachineKindName(DictBizCache.getValue("machine_kind", vo.getSupportMachineKind()));
            vo.setFixedMachinePositionName(DictBizCache.getValue("machine_position", vo.getFixedMachinePosition()));
            for (StartedCabinVO cabin : vo.getAllCabins()) {
                cabin.setCabinAndHatchName(DictBizCache.getValue("hold", cabin.getCabin()) + DictBizCache.getValue("hold_position", cabin.getHatch()));
            }
        }
        return data;
    }

}
